//
//  FTProfileHeaderView.m
//  FaceTo
//
//  Created by 姚冬元 on 2017/7/30.
//  Copyright © 2017年 chengbangming. All rights reserved.
//

#import "FTProfileHeaderView.h"


#define  WaterMaxHeight 170

@implementation FTProfileHeaderView

- (instancetype)init
{
    self = [super init];
    
    if(self){
        [self initUI];
    }
    return self;
}

- (void)initUI
{
    self.backgroundColor = FTColor;
    
    waterHeight = (1-0.1)*WaterMaxHeight;
    
    /// 第一层水波纹view ===========================
    self.waveView = [[UIView alloc] init];
    self.waveView.frame = CGRectMake(0, 0, kSCREEN_WIDTH, WaterMaxHeight);
    [self addSubview:self.waveView];
    self.waveView.backgroundColor = [UIColor colorWithRed:255/255.0f green:255/255.0f  blue:255/255.0f  alpha:0.8];
    
    /// 第二层水波纹 view =======================
    self.waveView2 = [[UIView alloc] init];
    self.waveView2.frame = CGRectMake(0, 0, kSCREEN_WIDTH, WaterMaxHeight);
    [self addSubview:self.waveView2];
    self.waveView2.backgroundColor = [UIColor colorWithRed:255/255.0f  green:255/255.0f  blue:255/255.0f  alpha:0.7];
    
    /// 第3层水波纹 view =======================
//    self.waveView3 = [[UIView alloc] init];
//    self.waveView3.frame = CGRectMake(0, 0, kSCREEN_WIDTH, WaterMaxHeight);
//    [self addSubview:self.waveView3];
//    self.waveView3.backgroundColor = [UIColor colorWithRed:255/255.0f  green:255/255.0f  blue:255/255.0f  alpha:0.7];
//    
    self.waterTimer = [NSTimer scheduledTimerWithTimeInterval:0.18 target:self selector:@selector(waterAction) userInfo:nil repeats:YES];
    self.waterTimer2 = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(waterAction2) userInfo:nil repeats:YES];
//    self.waterTimer3 = [NSTimer scheduledTimerWithTimeInterval:0.22 target:self selector:@selector(waterAction3) userInfo:nil repeats:YES];
    
    CGFloat avatarSize = iphone6p || iphone6 ? 70 : 60;
    
    _avatar = [[UIImageView alloc] init];
    _avatar.image = [UIImage imageNamed:@"Icon-120"];
    _avatar.layer.cornerRadius = avatarSize/2;
    _avatar.layer.masksToBounds = YES;
    [self addSubview:_avatar];
    
    _namelbl = [[UILabel alloc] init];
    _namelbl.text = @"一只大河马";
    _namelbl.textColor = FontColor;
    _namelbl.font = [UIFont boldSystemFontOfSize:16];
    [self addSubview:_namelbl];
    
    _ageBtn = [[UIButton alloc] init];
    [_ageBtn setTitle:@"21" forState:UIControlStateNormal];
    [_ageBtn setTitleColor:FontColor forState:UIControlStateNormal];
    [_ageBtn setImage:[UIImage imageNamed:@"girl"] forState:UIControlStateNormal];
    _ageBtn.titleLabel.font = LPFont(13);
    _ageBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 5);
    [self addSubview:_ageBtn];
    
    _signlbl = [[UILabel alloc] init];
    _signlbl.text = @"我是一只大河马，灰灰的，胖胖的，笨笨的";
    _signlbl.textColor = FontColor;
    _signlbl.font = LPFont(11);
    [self addSubview:_signlbl];
    
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = AlphaColor(255, 255, 255, 50);
    bgView.layer.cornerRadius = 15;
    bgView.layer.masksToBounds = YES;
    [self addSubview:bgView];
    
    UILabel *lbl = [[UILabel alloc] init];
    lbl.text = @"累计时间：";
    lbl.textColor = FontColor;
    lbl.font = LPFont(12);
    [bgView addSubview:lbl];
    
    _totalTimelbl = [[UILabel alloc] init];
    _totalTimelbl.text = @"15:23:51";
    _totalTimelbl.textColor = FontColor;
    _totalTimelbl.font = LPFont(16);
    [bgView addSubview:_totalTimelbl];
    
    [_avatar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20);
        make.centerY.equalTo(self);
        make.height.mas_equalTo(avatarSize);
        make.width.mas_equalTo(avatarSize);
    }];
    
    [_namelbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_avatar.mas_right).with.offset(20);
        make.bottom.equalTo(self.mas_centerY).with.offset(-5);
    }];
    
    [_ageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_namelbl.mas_right).with.offset(20);
        make.bottom.equalTo(_namelbl.mas_bottom);
    }];
    
    [_signlbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_avatar.mas_right).with.offset(20);
        make.top.equalTo(self.mas_centerY).with.offset(5);
    }];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(13);
        make.top.offset(15);
        make.width.mas_equalTo(170);
        make.height.mas_equalTo(35);
    }];
    
    [_totalTimelbl mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(lbl.mas_right).with.offset(5);
        make.right.offset(-30);
        make.centerY.equalTo(bgView.mas_centerY);
    }];
    
    [lbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_totalTimelbl.mas_left).offset(-5);
        make.centerY.equalTo(bgView.mas_centerY);
    }];
}

- (void)waterAction
{
    CGMutablePathRef wavePath = CGPathCreateMutable();
    CGPathMoveToPoint(wavePath, nil, 0,-WaterMaxHeight*0.5);
    float y = 0;
    _horizontal += 0.25;
    for (float x = 0; x <= self.waveView.frame.size.width; x++) {
        //峰高* sin(x * M_PI / self.frame.size.width * 峰的数量 + 移动速度)
        y = 10 * sin(x * M_PI / self.waveView.frame.size.width * 2 - _horizontal) ;
        CGPathAddLineToPoint(wavePath, nil, x, y+waterHeight);
    }
    CGPathAddLineToPoint(wavePath, nil, self.waveView.frame.size.width , WaterMaxHeight*0.5);
    CGPathAddLineToPoint(wavePath, nil, self.waveView.frame.size.width, WaterMaxHeight);
    CGPathAddLineToPoint(wavePath, nil, 0, WaterMaxHeight);
    [self.waveView setShape:wavePath];
}

- (void)waterAction2
{
    CGMutablePathRef wavePath2 = CGPathCreateMutable();
    CGPathMoveToPoint(wavePath2, nil, 0,-WaterMaxHeight*0.5);
    float y2 = 0;
    _horizontal2 += 0.5;
    for (float x2 = 0; x2 <= self.waveView2.frame.size.width; x2++) {
        //峰高* sin(x * M_PI / self.frame.size.width * 峰的数量 + 移动速度)
        y2 = -7* cos(x2 * M_PI / self.waveView2.frame.size.width * 2 + _horizontal2) ;
        CGPathAddLineToPoint(wavePath2, nil, x2, y2+waterHeight);
    }
    CGPathAddLineToPoint(wavePath2, nil, self.waveView2.frame.size.width , WaterMaxHeight*0.5);
    CGPathAddLineToPoint(wavePath2, nil, self.waveView2.frame.size.width, WaterMaxHeight);
    CGPathAddLineToPoint(wavePath2, nil, 0, WaterMaxHeight);
    [self.waveView2 setShape:wavePath2];
}

//- (void)waterAction3
//{
//    CGMutablePathRef wavePath3 = CGPathCreateMutable();
//    CGPathMoveToPoint(wavePath3, nil, 0,-WaterMaxHeight*0.5);
//    float y3 = 0;
//    _horizontal3 += 0.7;
//    for (float x3 = 0; x3 <= self.waveView3.frame.size.width; x3++) {
//        //峰高* sin(x * M_PI / self.frame.size.width * 峰的数量 + 移动速度)
//        y3 = 5* cos(x3 * M_PI / self.waveView3.frame.size.width * 2 + _horizontal3) ;
//        CGPathAddLineToPoint(wavePath3, nil, x3, y3+waterHeight);
//    }
//    CGPathAddLineToPoint(wavePath3, nil, self.waveView3.frame.size.width , WaterMaxHeight*0.5);
//    CGPathAddLineToPoint(wavePath3, nil, self.waveView3.frame.size.width, WaterMaxHeight);
//    CGPathAddLineToPoint(wavePath3, nil, 0, WaterMaxHeight);
//    [self.waveView3 setShape:wavePath3];
//}

@end
